{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、numpy中的一般语法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 4 5]\n",
      " [7 8 9]]\n",
      "矩阵的逆:\n",
      "[[-1.08333333  0.58333333]\n",
      " [-0.08333333  0.08333333]\n",
      " [ 0.91666667 -0.41666667]]\n",
      "矩阵的转置:\n",
      "[[3 7]\n",
      " [4 8]\n",
      " [5 9]]\n",
      "矩阵的秩:\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 矩阵的逆、转置及秩\n",
    "data_1 = np.matrix('3 4 5;7 8 9')\n",
    "print(data_1)\n",
    "print(\"矩阵的逆:\")\n",
    "data_2 = data_1.I\n",
    "print(data_2)\n",
    "print(\"矩阵的转置:\")\n",
    "data_3 = data_1.T\n",
    "print(data_3)\n",
    "print(\"矩阵的秩:\")\n",
    "data_4 = np.linalg.matrix_rank(data_1)\n",
    "print(data_4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "# 矩阵的维度（也称为轴数）\n",
    "print(data_1.ndim)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、数组的创建\n",
    "    1. 创建二维数组\n",
    "        这里强调：使用matrix只能创建二维矩阵，而是用非matrix方法可以创建高维矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]]\n",
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "# 全0的矩阵\n",
    "arr_1 = np.matrix(np.zeros((3, 3)))\n",
    "print(arr_1)\n",
    "# 或者\n",
    "arr_1 = np.zeros((3, 3))\n",
    "print(arr_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]]\n",
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "# 全1的矩阵\n",
    "arr_2 = np.matrix(np.ones((3, 3)))\n",
    "print(arr_2)\n",
    "# 或者\n",
    "arr_2 = np.ones((3, 3))\n",
    "print(arr_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.40602889, 0.03820256, 0.6396352 , 0.22960063],\n",
       "        [0.57794841, 0.76032408, 0.30065963, 0.91668478],\n",
       "        [0.66738645, 0.89344645, 0.33347352, 0.94924906]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建随机矩阵\n",
    "arr_3 = np.matrix(np.random.rand(3, 4))\n",
    "arr_3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[5, 5, 5, 9],\n",
       "        [9, 5, 7, 5],\n",
       "        [5, 7, 7, 6]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建整数随机矩阵\n",
    "arr_4 = np.matrix(np.random.randint(5, 10, size=(3, 4)))\n",
    "arr_4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[3, 0, 0, 0],\n",
       "        [0, 4, 0, 0],\n",
       "        [0, 0, 5, 0],\n",
       "        [0, 0, 0, 6]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建对角矩阵\n",
    "new_list = [i for i in range(3, 7)]\n",
    "arr_5 = np.matrix(np.diag(new_list))\n",
    "arr_5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建单位矩阵\n",
    "arr_6 = np.eye(3)\n",
    "arr_6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 5 5 9]\n",
      " [9 5 7 5]\n",
      " [5 7 7 6]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "matrix([[5, 5, 5],\n",
       "        [9, 9, 5],\n",
       "        [7, 5, 5],\n",
       "        [7, 7, 6]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 改变矩阵的形状\n",
    "print(arr_4)\n",
    "arr_7 = arr_4.reshape(4, 3)\n",
    "arr_7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    2. 高维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "数组维度：3\n"
     ]
    }
   ],
   "source": [
    "# 例如创建3维的数组\n",
    "arr_8 = np.arange(24).reshape(2, 3, 4)\n",
    "\n",
    "print(arr_8)\n",
    "print(\"数组维度：\"+str(arr_8.ndim))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 三、numpy的索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "单独获取到一个值\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "print(arr_8)\n",
    "print(\"单独获取到一个值\")\n",
    "print(arr_8[0,0,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "获取到深度为0的所有值\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "print(arr_8)\n",
    "print(\"获取到深度为0的所有值\")\n",
    "print(arr_8[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "获取到深度为0的前两行的值\n",
      "[[0 1 2 3]\n",
      " [4 5 6 7]]\n"
     ]
    }
   ],
   "source": [
    "print(arr_8)\n",
    "print(\"获取到深度为0的前两行的值\")\n",
    "print(arr_8[0,:2,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "获取到深度为0的前两行后两列的值\n",
      "[[2 3]\n",
      " [6 7]]\n"
     ]
    }
   ],
   "source": [
    "print(arr_8)\n",
    "print(\"获取到深度为0的前两行后两列的值\")\n",
    "print(arr_8[0,:2,-2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "获取到深度为0的前两行后两列的值并替换为-2\n",
      "获取到深度为0的前两行后两列的值\n",
      "[[2 3]\n",
      " [6 7]]\n",
      "替换为-2\n",
      "原来数组\n",
      "[[[ 0  1 -2 -2]\n",
      "  [ 4  5 -2 -2]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n"
     ]
    }
   ],
   "source": [
    "print(arr_8)\n",
    "print(\"获取到深度为0的前两行后两列的值并替换为-2\")\n",
    "print(\"获取到深度为0的前两行后两列的值\")\n",
    "print(arr_8[0,:2,-2:])\n",
    "print(\"替换为-2\")\n",
    "arr_8[0,:2,-2:] = -2\n",
    "print(\"原来数组\")\n",
    "print(arr_8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## numpy的合并功能\n",
    "     1. 在第一轴合并\n",
    "     2. 在第二轴合并\n",
    "     3. 在第三轴合并\n",
    "     4. 在任意轴合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "合并前的arr_8\n",
      "[[[ 0  1 -2 -2]\n",
      "  [ 4  5 -2 -2]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "两个arr_8合并后\n",
      "[[[ 0  1 -2 -2]\n",
      "  [ 4  5 -2 -2]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]\n",
      "\n",
      " [[ 0  1 -2 -2]\n",
      "  [ 4  5 -2 -2]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n"
     ]
    }
   ],
   "source": [
    "# 1. 在第一轴合并\n",
    "# vstack\n",
    "print(\"合并前的arr_8\\n\"+str(arr_8))\n",
    "temp_1 = arr_8\n",
    "temp_2 = arr_8\n",
    "temp = np.vstack((temp_1, temp_2))\n",
    "print(\"两个arr_8合并后\\n\"+str(temp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "合并前的arr_8\n",
      "[[[ 0  1 -2 -2]\n",
      "  [ 4  5 -2 -2]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "两个arr_8合并后\n",
      "[[[ 0  1 -2 -2]\n",
      "  [ 4  5 -2 -2]\n",
      "  [ 8  9 10 11]\n",
      "  [ 0  1 -2 -2]\n",
      "  [ 4  5 -2 -2]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n"
     ]
    }
   ],
   "source": [
    "# 2. 在第二轴合并\n",
    "# hstack\n",
    "print(\"合并前的arr_8\\n\"+str(arr_8))\n",
    "temp_1 = arr_8\n",
    "temp_2 = arr_8\n",
    "temp = np.hstack((temp_1, temp_2))\n",
    "print(\"两个arr_8合并后\\n\"+str(temp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "合并前的arr_8\n",
      "[[[ 0  1 -2 -2]\n",
      "  [ 4  5 -2 -2]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "两个arr_8合并后\n",
      "[[[ 0  1 -2 -2  0  1 -2 -2]\n",
      "  [ 4  5 -2 -2  4  5 -2 -2]\n",
      "  [ 8  9 10 11  8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15 12 13 14 15]\n",
      "  [16 17 18 19 16 17 18 19]\n",
      "  [20 21 22 23 20 21 22 23]]]\n"
     ]
    }
   ],
   "source": [
    "# 3. 在第三轴合并\n",
    "# dstack\n",
    "print(\"合并前的arr_8\\n\"+str(arr_8))\n",
    "temp_1 = arr_8\n",
    "temp_2 = arr_8\n",
    "temp = np.dstack((temp_1, temp_2))\n",
    "print(\"两个arr_8合并后\\n\"+str(temp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "合并前的arr_8\n",
      "[[[ 0  1 -2 -2]\n",
      "  [ 4  5 -2 -2]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "两个arr_8合并后\n",
      "[[[ 0  1 -2 -2  0  1 -2 -2]\n",
      "  [ 4  5 -2 -2  4  5 -2 -2]\n",
      "  [ 8  9 10 11  8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15 12 13 14 15]\n",
      "  [16 17 18 19 16 17 18 19]\n",
      "  [20 21 22 23 20 21 22 23]]]\n"
     ]
    }
   ],
   "source": [
    "# 4. 任意轴合并\n",
    "# concatenate\n",
    "print(\"合并前的arr_8\\n\"+str(arr_8))\n",
    "temp_1 = arr_8\n",
    "temp_2 = arr_8\n",
    "# axis表示设置在何处轴合并，默认在第一轴\n",
    "temp = np.concatenate((arr_8, arr_8), axis=2)\n",
    "print(\"两个arr_8合并后\\n\"+str(temp))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
